<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery.min.js"></script>
</head>
<body>
   <h1>管理员对用户授权</h1>  
   <select id="usersel">
      <option value="0">请选择用户</option>
   	  <c:forEach items="${userlist}" var="u">
   	  	<option value="${u.uid }">${u.uname }</option>
   	  </c:forEach>
   </select>
   <hr/>
   <h3>所有权限</h3>
   <c:forEach items="${authlist}" var="au">
   	  <input class="auth" type="checkbox" value="${au.authId }" />${au.authName}--${au.authPath}<br>
   </c:forEach>
   <hr/>
  <input type="button" value="授权" />
   
</body>

<script>
// onload
$(function(){
	//下拉列表绑定事件 -- 改变时触发
   $("#usersel").change(function(){
	   //先找到所有权利的复选框
		var auths = $(".auth");  
		//清空所有的勾选
		auths.attr("checked",false);
		var uid = $("#usersel").val() ; // 用户ID
		if(uid==0){ return }
		
		//发出ajax 请求
		$.post("getauth",{"userid":uid},function(data){
			console.log(data);
			
			var t=false;
			for(i=0;i<auths.length;i++){
				var authid =$(auths[i]).val();   //权利ID		
				 t=false;
				 for(j=0;j<data.length;j++){ // 查找你是否有这个权利
					 if(data[j].authId==parseInt(authid)){
						 t=true;
						 break;
					 }
				 }  
				if(t){
					$(auths[i]).attr("checked",true); //选中复选框
				} 
			}  
			
		});
		
   });

	
});


</script>
</html>